'use strict';

const Controller = require('egg').Controller;

class ymqMenuController extends Controller {

    //根据角色id查询其访问的路径权限，菜单权限
    async querybyroleurl() {
            let emp = this.ctx.request.body;
            // 调用service 的方法
            let result = await this.service.ymqMenuser.queryroleidbyurl(emp);

            function arrayToTree(result, parentId) {    //  arr 是返回的数据  parendId 父id
                  
                let temp = [];  
                let treeArr = result;  
                treeArr.forEach((item, index) => {     if (item.pid == parentId) {       if (arrayToTree(treeArr, treeArr[index].id).length > 0) {          // 递归调用此函数
                                    treeArr[index].children =  arrayToTree(treeArr, treeArr[index].id);       }      
                        temp.push(treeArr[index]);     }   });  
                return temp;
            }
            this.ctx.body = {
                statuscode: 200,
                title: '查询成功',
                affectrow: result.affectedRows,
                ...arrayToTree(result , 0)
            }
        }
        //根据角色id查询其权限,渲染角色的权限树
    async querybyroleid() {
        let emp = this.ctx.request.body;
        // 调用service 的方法
        let result = await this.service.ymqMenuser.queryuserrole(emp);

        function arrayToTree(result, parentId) {    //  arr 是返回的数据  parendId 父id
              
            let temp = [];  
            let treeArr = result;  
            treeArr.forEach((item, index) => {     if (item.parent_id == parentId) {       if (arrayToTree(treeArr, treeArr[index].id).length > 0) {          // 递归调用此函数
                               treeArr[index].children =  arrayToTree(treeArr, treeArr[index].id);       }      
                    temp.push(treeArr[index]);     }   });  
            return temp;
        }
        this.ctx.body = {
            statuscode: 200,
            title: '查询成功',
            affectrow: result.affectedRows,
            ...arrayToTree(result , 0)
        }
    }
}
module.exports = ymqMenuController;